<?php

class Account_Form_Register extends Zend_Form
{
	public function init()
	{
		parent::init();

        // Set the method for the display form to POST
		$this
			->setAction('')
			->setMethod('post')
		;

		$stripSlashesFilter = new Euso_Filter_StripSlashes();

		// Create and configure idname element
		$idname = new Zend_Form_Element_Text('idname');
		$idnameRegex = new Zend_Validate_Regex('/^[a-zA-Z0-9\_\-\.]*$/');
		$idnameRegex->setMessage(_('Use only combination of alphabets, numbers, hyphen, dot and underscorce'));
		$idname
			->setLabel('Username:')
			->setRequired(true)
			->addFilter($stripSlashesFilter)
			->addFilter('StringTrim')
			->addValidator('stringLength', false, array(4,30))
			->addValidator($idnameRegex)
			->setAttrib('size', 36)
			->setAttrib('maxLength', 30)
		;
		$this->addElement($idname);

		// Create and configure email element
		$email = new Zend_Form_Element_Text('email');
		$email
			->setLabel('Email:')
			->setRequired(true)
			->addFilter($stripSlashesFilter)
			->addFilter('StringTrim')
			->addFilter('StringToLower')
			->addValidator('EmailAddress')
			->setAttrib('size', 36)
		;
		$this->addElement($email);

		// Create and configure confirm email element
		$email2 = new Zend_Form_Element_Text('email_confirm');
		$emailsMatch = new Euso_Validate_FieldsMatch('email');
		$emailsMatch->setMessage(_('Emails do not match'));
		$email2
			->setLabel('Confirm Email:')
			->setRequired(true)
			->addFilter($stripSlashesFilter)
			->addFilter('StringTrim')
			->addValidator($emailsMatch)
			->setAttrib('size', 36)
		;
		$this->addElement($email2);

		// Create and configure password element
		$password = new Zend_Form_Element_Password('password');
		$password
			->setLabel('Password:')
			->setRequired(true)
			->addFilter($stripSlashesFilter)
			->addFilter('StringTrim')
			->addValidator('StringLength', false, array(6,20))
			->setAttrib('size', 36)
			->setAttrib('maxLength', 20)
		;
		$this->addElement($password);

		// Create and configure confirm password element
		$password2 = new Zend_Form_Element_Password('password_confirm');
		$passwordsMatch = new Euso_Validate_FieldsMatch('password');
		$passwordsMatch->setMessage(_('Passwords do not match'));
		$password2
			->setLabel('Confirm Password:')
			->setRequired(true)
			->addFilter($stripSlashesFilter)
			->addFilter('StringTrim')
			->addValidator($passwordsMatch)
			->setAttrib('size', 36)
			->setAttrib('maxLength', 20)
		;
		$this->addElement($password2);


		$opts = Kernel::getInstance()->getOptions();

		$activationMethod = 'admin';
		$messageRequired = FALSE;
		$captchaEnabled = FALSE;
		if (is_array($opts['account'])) {
			$accOpts = $opts['account'];
			if (is_array($accOpts['activation']) && isset($accOpts['activation']['method'])) {
				$activationMethod = $accOpts['activation']['method'];
			}
			if ((is_array($accOpts['registration']) && intval($accOpts['registration']['message']) === 1)
				|| $activationMethod === 'admin') {
				$messageRequired = TRUE;
			}
			if (is_array($accOpts['registration']) && intval($accOpts['registration']['recaptcha']) === 1) {
				$captchaEnabled = TRUE;
			}
		}

		if ($messageRequired === TRUE) {
			$elem = new Zend_Form_Element_TextArea('reg_message');
			$elem
				->setLabel('Message:')
				->setRequired(true)
				->addFilter($stripSlashesFilter)
				->addFilter('StringTrim')
				->setAttrib('rows', 4)
				->setAttrib('cols', 64)
				->setAttrib('escape', false)
			;
			$this->addElement($elem);
		}

		if ($captchaEnabled === TRUE) {
			if (is_array($opts['site']) && is_array($opts['site']['recaptcha'])
				&& is_array($opts['site']['recaptcha']['key'])) {
				$recaptchaPub = $opts['site']['recaptcha']['key']['public'];
				$recaptchaPvt = $opts['site']['recaptcha']['key']['private'];
				// [re]CAPTCHA
				$captcha = new Zend_Form_Element_Captcha('challenge', array(
					'captcha' => 'ReCaptcha',
					'captchaOptions' => array(
						'captcha' => 'ReCaptcha',
						'privKey' => $recaptchaPvt,
						'pubKey' => $recaptchaPub)
					)
				);
				$recaptchaTheme = $opts['site']['recaptcha']['theme'];
				if (empty($recaptchaTheme) === FALSE) {
					$captcha->getCaptcha()->getService()->setOption('theme', $recaptchaTheme);
				}
				$this->addElement($captcha);
			}
		}


		//TODO: ToS, privacy policy

        // Add the submit button
		$submit = new Zend_Form_Element_Submit('submit');
		$submit
			->setLabel('Register')
			->setIgnore(true)
			->setAttrib('class', 'submit')
		;
		$this->addElement($submit);
	}
}
